/*
 * PasswordValueDisplay.java
 *
 * Created on 26 January 2004, 02:02
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.user;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.NodeUtil;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.UserPK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PasswordIncorrectException;
import com.velocityme.utility.PermissionDeniedException;
import java.rmi.RemoteException;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Robert Crida Work
 */
public class PasswordControl extends javax.swing.JPanel {
    
    private UserPK m_userPK;
    private KeySession m_key;
    private Set m_setPermissions;

    /** Creates new form PasswordValueDisplay */
    public PasswordControl(KeySession p_key, UserPK p_userPK, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_userPK = p_userPK;
        m_setPermissions = p_setPermissions;
        try {
            jTextFieldUser.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, new NodePK(m_userPK.getUserId())));
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        if (p_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.USER_CHANGE_PASSWORD)))) {
            jLabelOldPassword.setVisible(false);
            jPasswordFieldOldPassword.setVisible(false);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabelUser = new javax.swing.JLabel();
        jTextFieldUser = new javax.swing.JTextField();
        jLabelOldPassword = new javax.swing.JLabel();
        jPasswordFieldOldPassword = new javax.swing.JPasswordField();
        jLabelNewPassword = new javax.swing.JLabel();
        jPasswordFieldNewPassword = new javax.swing.JPasswordField();
        jLabelRepeatPassword = new javax.swing.JLabel();
        jPasswordFieldRepeatPassword = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jButtonApply = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabelUser.setText("User:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabelUser, gridBagConstraints);

        jTextFieldUser.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jTextFieldUser, gridBagConstraints);

        jLabelOldPassword.setText("Old Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabelOldPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jPasswordFieldOldPassword, gridBagConstraints);

        jLabelNewPassword.setText("New Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabelNewPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jPasswordFieldNewPassword, gridBagConstraints);

        jLabelRepeatPassword.setText("Repeat Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabelRepeatPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jPasswordFieldRepeatPassword, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonApply.setText("Apply");
        jButtonApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonApplyActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonApply);

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
        // Add your handling code here:
        try {
            String oldPassword = jPasswordFieldOldPassword.getText();
            String newPassword = jPasswordFieldNewPassword.getText();
            String repeatPassword = jPasswordFieldRepeatPassword.getText();
            if (newPassword.equals(repeatPassword)) {
                try {
                    StatelessRemoteClientSessionUtil.getSession().userChangePassword(m_key, m_userPK, oldPassword, newPassword);
                    getParent().remove(this);
                }
                catch(PermissionDeniedException e) {
                    JOptionPane.showMessageDialog(this, "You don't have permission to change the password.", "Change failed", JOptionPane.WARNING_MESSAGE);
                }
                catch(PasswordIncorrectException e) {
                    JOptionPane.showMessageDialog(this, "Old password doesn't match existing one.", "Change failed", JOptionPane.WARNING_MESSAGE);
                }
            }
            else {
                JOptionPane.showMessageDialog(this, "New and repeat passwords must be equal.", "Change password", JOptionPane.WARNING_MESSAGE);
            }
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonApplyActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonApply;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JLabel jLabelNewPassword;
    private javax.swing.JLabel jLabelOldPassword;
    private javax.swing.JLabel jLabelRepeatPassword;
    private javax.swing.JLabel jLabelUser;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordFieldNewPassword;
    private javax.swing.JPasswordField jPasswordFieldOldPassword;
    private javax.swing.JPasswordField jPasswordFieldRepeatPassword;
    private javax.swing.JTextField jTextFieldUser;
    // End of variables declaration//GEN-END:variables
    
}
